package org.leetcode.hard.leetcode51;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {


    /**
     * row:多少行数，可以理解为长度
     * col：多少列数，可以理解为宽度
     */

    List<List<String>> res = new ArrayList<>();

    public List<List<String>> solveNQueens(int n) {

        char[][] chessboard = new char[n][n];
        for (char[] c : chessboard) {
            Arrays.fill(c, '.');
        }
        backTrack(n, 0, chessboard);
        return res;
    }


    private void backtracking(int n, int row, char[][] chessboard) {
        if (row == chessboard.length) {
            res.add(arrToList(chessboard));
            return;
        }

        for (int i = 0; i < chessboard[0].length; i++) {
            if (isValid(row, i, n, chessboard)) {

                chessboard[row][i] = 'Q';

                backtracking(n, row + 1, chessboard);

                chessboard[row][i] = '.';
            }
        }
    }

    private List<String> arrToList(char[][] chessboard) {

        List<String> list = new ArrayList<>();

        for (int i = 0; i < chessboard.length; i++) {
            for (int j = 0; j < chessboard[0].length; j++) {
                list.add(String.valueOf(chessboard[i][j]));
            }
        }
        return list;
    }


    private boolean isValid2(int row,int col,char [] [] chessboard){

        int n = chessboard.length;

        //判断同一列
        for (int i = 0; i <=row; i++) {
            if (chessboard[row][col]=='Q'){
                return false;
            }
        }

        //判断45度

        for (int i = row-1,j=col-1;i>=0 && j<n;i--,j++){
            if (chessboard[i][j]=='Q'){
                return false;
            }
        }

        //判断135度
        for (int i = row-1,j=col-1;i>=0 && j>=0;i--,j--){
            if (chessboard[i][j]=='Q'){
                return false;
            }
        }

        return true;
    }


    public void backTrack2(int n, int row, char[][] chessboard) {
        if (n == row) {
            res.add(Array2List(chessboard));
            return;
        }

        for (int col = 0; col < n; col++) {
            if (isValid(row, col, n, chessboard)) {
                chessboard[row][col] = 'Q';
                backTrack(n, row + 1, chessboard);
                chessboard[row][col] = '.';
            }
        }
    }

    private List Array2List2(char[][] chessbord) {
        List<String> list = new ArrayList<>();
        for (char[] temp : chessbord) {
            list.add(String.copyValueOf(temp));
        }

        return list;
    }

    public void backTrack(int n, int row, char[][] chessboard) {
        if (row == n) {
            res.add(Array2List(chessboard));
            return;
        }

        for (int col = 0; col < n; ++col) {
            if (isValid(row, col, n, chessboard)) {
                chessboard[col][row] = 'Q';
                backTrack(n, row + 1, chessboard);
                chessboard[col][row] = '.';
            }
        }

    }

    public List Array2List(char[][] chessboard) {
        List<String> list = new ArrayList<>();

        for (char[] c : chessboard) {
            list.add(String.copyValueOf(c));
        }
        return list;
    }


    public boolean isValid2(int row, int col, int n, char[][] chessboard) {
        //判断每一列

        for (int i = 0; i < row; i++) {
            if (chessboard[i][col] == 'Q') {
                return false;
            }
        }

        //判断45度
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (chessboard[i][j] == 'Q') {
                return false;
            }
        }

        //判断135度

        for (int i = row - 1, j = col + 1; i >= 0 && j <= n - 1; i--, j++) {
            if (chessboard[i][j] == 'Q') {
                return false;
            }
        }

        return true;

    }

    public boolean isValid(int row, int col, int n, char[][] chessboard) {
        // 检查列
        for (int i = 0; i < row; ++i) { // 相当于剪枝
            if (chessboard[i][col] == 'Q') {
                return false;
            }
        }

        // 检查45度对角线
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (chessboard[i][j] == 'Q') {
                return false;
            }
        }

        // 检查135度对角线
        for (int i = row - 1, j = col + 1; i >= 0 && j <= n - 1; i--, j++) {
            if (chessboard[i][j] == 'Q') {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[][] chessboard = new int[3][3];

        int number = 1; // 开始填充的数字

        for (int i = 0; i < chessboard.length; i++) { // 遍历行
            for (int j = 0; j < chessboard[i].length; j++) { // 遍历列
                chessboard[i][j] = number++; // 填充数字，并将数字递增
            }
        }

        // 打印填充后的数组以验证
//        for (int i = 0; i < chessboard.length; i++) {
//            for (int j = 0; j < chessboard[i].length; j++) {
//                System.out.print(chessboard[i][j] + " ");
//            }
//            System.out.println(); // 每打印完一行后换行
//        }

        System.out.println(chessboard[2][1]);
        System.out.println(chessboard[1][2]);


    }

}
